package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.UUIDHelper;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.system.domain.ByDb15;
import com.ruoyi.system.domain.ByMyStandard;
import com.ruoyi.system.domain.ByOrders;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ByShopcarMapper;
import com.ruoyi.system.domain.ByShopcar;

/**
 * ByShopCarService业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-14
 */
@Service
public class ByShopcarServiceImpl implements IByShopcarService 
{
    @Autowired
    private ByShopcarMapper byShopcarMapper;

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private IByMyStandardService iByMyStandardService;

    @Autowired
    private IByOrdersService iByOrdersService;

    @Autowired
    private IEsService esService;

    /**
     * 查询ByShopCar
     * 
     * @param id ByShopCar主键
     * @return ByShopCar
     */
    @Override
    public ByShopcar selectByShopcarById(String id)
    {
        return byShopcarMapper.selectByShopcarById(id);
    }

    /**
     * 查询ByShopCar列表
     * 
     * @param byShopcar ByShopCar
     * @return ByShopCar
     */
    @Override
    public List<ByShopcar> selectByShopcarList(ByShopcar byShopcar)
    {
        return byShopcarMapper.selectByShopcarList(byShopcar);
    }

    @Override
    public TableDataInfo selectByShopcarUserList(String userId,int pageNum,int pageSize) {
        List<ByShopcar> list = byShopcarMapper.selectByShopcarUserList(userId);
        List<ByDb15> listDb = new ArrayList<>();
        list.forEach(element ->{
            ByDb15 byDb15 = esService.getDetail(element.getStandardId());
            if(byDb15!=null){
                listDb.add(byDb15);
            }
        });
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        int total = listDb.size();
        int start = (pageNum - 1) * pageSize;
        int end = pageNum * pageSize;
        if(end > listDb.size()){
            end = listDb.size();
        }
        rspData.setRows(listDb.subList(start,end));
        rspData.setTotal(total);
        return rspData;
    }

    /**
     * 新增ByShopCar
     * 
     * @param byShopcar ByShopCar
     * @return 结果
     */
    @Override
    public AjaxResult insertByShopcar(ByShopcar byShopcar)
    {
        //查询重复
        byShopcar.setUserId(SecurityUtils.getUserId());
        List<ByShopcar> list = byShopcarMapper.selectByShopcarList(byShopcar);
        if(list.size()>0){
            return AjaxResult.error("请勿重复添加");
        }else{
            byShopcar.setId(UUIDHelper.get32UUID());
            byShopcar.setCareteBy(SecurityUtils.getUsername());
            byShopcar.setCraeteTime(DateTime.now());
            byShopcar.setIsDelete(0L);
            byShopcar.setUserId(SecurityUtils.getUserId());
            byShopcarMapper.insertByShopcar(byShopcar);
            return AjaxResult.success("添加成功");
        }
    }

    /**
     * 修改ByShopCar
     * 
     * @param byShopcar ByShopCar
     * @return 结果
     */
    @Override
    public int updateByShopcar(ByShopcar byShopcar)
    {
        byShopcar.setUpdateTime(DateUtils.getNowDate());
        return byShopcarMapper.updateByShopcar(byShopcar);
    }

    /**
     * 批量删除ByShopCar
     * 
     * @param ids 需要删除的ByShopCar主键
     * @return 结果
     */
    @Override
    public int deleteByShopcarByIds(String[] ids)
    {
        return byShopcarMapper.deleteByShopcarByIds(ids);
    }

    /**
     * 删除ByShopCar信息
     * 
     * @param id ByShopCar主键
     * @return 结果
     */
    @Override
    public int deleteByShopcarById(String id)
    {
        return byShopcarMapper.deleteByShopcarById(id);
    }

    @Override
    public int deleteByShopcarByStandard(String userId, String standardId) {
        return byShopcarMapper.deleteByShopcarByStandard(userId,standardId);
    }

    @Override
    public AjaxResult buyStandard(List<ByDb15> list) {
        //添加订单
        ByOrders byOrders = new ByOrders();
        byOrders.setCreateTime(DateTime.now());
        byOrders.setCreateDate(DateTime.now().toString());
        byOrders.setCreateBy(SecurityUtils.getUsername());
        byOrders.setUpdateTime(DateTime.now());
        byOrders.setUpdateDate(DateTime.now().toString());
        byOrders.setUpdateBy(SecurityUtils.getUsername());
        byOrders.setOrderName("标准购买");
        byOrders.setOrderNum(8L);
        byOrders.setOrderStatus("1");

        byOrders.setMemberEmail(SecurityUtils.getLoginUser().getUser().getEmail());
        byOrders.setMemberId(SecurityUtils.getUserId());
        byOrders.setMemberIp(IpUtils.getHostIp());
        byOrders.setMemberName(SecurityUtils.getUsername());
        byOrders.setMemberPhone(SecurityUtils.getLoginUser().getUser().getPhonenumber());
        SysUser sysUser = iSysUserService.selectUserById(SecurityUtils.getUserId());
        double userDeposit = Double.parseDouble(sysUser.getDeposit());
        double totlePrice = 0;
        for(ByDb15 byDb15:list){
            if(StringUtils.isNotEmpty(byDb15.getA310())){
                double itemPrice = Double.parseDouble(byDb15.getA310());
                totlePrice += itemPrice;
            }
        }
        byOrders.setPaidAmount(String.valueOf(totlePrice));
        if(totlePrice>userDeposit){
            return AjaxResult.error("余额不足，请联系管理员充值");
        }
        for(ByDb15 byDb15:list){
            //插入我的标准
            ByMyStandard byMyStandard = new ByMyStandard();
            byMyStandard.setId(UUIDHelper.get32UUID());
            byMyStandard.setStandardId(byDb15.getId());
            byMyStandard.setUserId(SecurityUtils.getUserId());
            byMyStandard.setA100(byDb15.getA100());
            byMyStandard.setCreateTime(DateTime.now());
            byMyStandard.setA298(byDb15.getA298());
            byMyStandard.setA894(byDb15.getFilePath());
            //订单信息处理
            byOrders.setId(UUIDHelper.get32UUID());
            byOrders.setStandardId(byDb15.getA100());
            byOrders.setStandardName(byDb15.getA298());
            iByMyStandardService.insertByMyStandard(byMyStandard);
            iByOrdersService.insertByOrders(byOrders);
            //删除购物车
            deleteByShopcarByStandard(SecurityUtils.getUserId(),byDb15.getId());
        }
        double finalDeposit = userDeposit - totlePrice;
        //更新余额
        sysUser.setUserId(SecurityUtils.getUserId());
        sysUser.setDeposit(finalDeposit+"");
        iSysUserService.updateUser(sysUser);
        return AjaxResult.success("购买成功");
    }
}
